package com.walker.dicegame;

import java.util.List;
import java.util.ArrayList;

import android.graphics.Color;
import android.graphics.Paint;
import android.view.Display;

import com.joew.framework.Game;
import com.joew.framework.Graphics;
import com.joew.framework.Image;
import com.joew.framework.Screen;
import com.joew.framework.Graphics.ImageFormat;
import com.joew.framework.Input.TouchEvent;

public class GameScreen extends Screen {
    enum GameState {
        Ready, Running, Paused, GameOver
    }

    GameState state = GameState.Ready;

    // Variable Setup
    // You would create game objects here.
    ArrayList<Dice> dice= new ArrayList<Dice>();
    Button AddDie = new Button(50, 200, 100, 100, "Add Die");
    Button Bin = new Button(800, 300, 200, 200, "Delete");
    int numDice = 3;
    int livesLeft = 1;
    Paint paint;

    public GameScreen(Game game) {
        super(game);      
        // Initialize game objects here
        
        for (int i= 0; i < numDice; i++)
            dice.add(new Dice(100*i, 100));

        // Defining a paint object
        paint = new Paint();
        paint.setTextSize(30);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        
//        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    }

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

        // We have four separate update methods in this example.
        // Depending on the state of the game, we call different update methods.
        // Refer to Unit 3's code. We did a similar thing without separating the
        // update methods.

        if (state == GameState.Ready)
            updateReady(touchEvents);
        if (state == GameState.Running)
            updateRunning(touchEvents, deltaTime);
        if (state == GameState.Paused)
            updatePaused(touchEvents);
        if (state == GameState.GameOver)
            updateGameOver(touchEvents);
    }

    private void updateReady(List<TouchEvent> touchEvents) {
        
        // This example starts with a "Ready" screen.
        // When the user touches the screen, the game begins. 
        // state now becomes GameState.Running.
        // Now the updateRunning() method will be called!
        
        if (touchEvents.size() > 0)
            state = GameState.Running;
    }

    private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
        
        //This is identical to the update() method from our Unit 2/3 game.
        
        
        // 1. All touch input is handled here:
        int len = touchEvents.size();
        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);

            if (event.type == TouchEvent.TOUCH_DOWN) {

                if (AddDie.contains(event.x, event.y) && (dice.size() < 10) ) {
                	dice.add(new Dice(dice.get(dice.size()-1).getx()+100, 100));
                }

                else if (event.x > 640) {
                	
                    // Move right.
                }
                
                for(int n = 0; n < dice.size(); n++){
                	Dice die = dice.get(n);
                	if (die.contains(event.x, event.y)){                	
                    	die.update();
                    }
                }
            }
            
            if (event.type == TouchEvent.TOUCH_HOLD){
            	// Try to stop the clipping here
            	boolean clipping = false;
            	for(int t = 0; t< dice.size(); t++){
            		Dice die = dice.get(t);
            		if (!die.contains(event.x, event.y) & die.clips(event.x, event.y)){
            			// So in this case we don't want to move the die
            			clipping = true;
            		}
            	}
            	if(!clipping){
	            	for(int n = 0; n < dice.size(); n++){
	                	Dice die = dice.get(n);
	                	if (die.contains(event.x, event.y)){                	
	                		die.setx(game.getInput().getTouchX(i));
			                die.sety(game.getInput().getTouchY(i));
			                if (Bin.contains(event.x, event.y) && dice.size() > 1){
			                	dice.remove(n);
			                }
	                    	break;
	                    }
	                }
            	}
            	
            }

            if (event.type == TouchEvent.TOUCH_UP) {

                if (event.x < 640) {
                    // Stop moving left.
                }

                else if (event.x > 640) {
                    // Stop moving right. }
                }
            }  
        }
        
        // 2. Check miscellaneous events like death:
        
        if (livesLeft == 0) {
            state = GameState.GameOver;
        }
        
        // 3. Call individual update() methods here.
        // This is where all the game updates happen.
        // For example, robot.update();
    }

    private void updatePaused(List<TouchEvent> touchEvents) {
        int len = touchEvents.size();
        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if (event.type == TouchEvent.TOUCH_DOWN) {
            	state = GameState.Running;
            }
        }
    }

    private void updateGameOver(List<TouchEvent> touchEvents) {
        int len = touchEvents.size();
        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if (event.type == TouchEvent.TOUCH_UP) {
                if (event.x > 300 && event.x < 980 && event.y > 100
                        && event.y < 500) {
                    nullify();
                    game.setScreen(new MainMenuScreen(game));
                    return;
                }
            }
        }

    }

    @Override
    public void paint(float deltaTime) {
        Graphics g = game.getGraphics();
        // Colours the screen green
        g.clearScreen(65280);
        
        // Eh, could probably put a menu here....
        g.drawRect(0, 0, g.getWidth()/5, g.getHeight()+1, Color.BLACK);
        // And a button?
        g.drawRect(AddDie.getx(), AddDie.gety(), AddDie.getwidth(), 
        		AddDie.getheight(), Color.WHITE);
        paint.setColor(Color.BLUE);
        g.drawString(AddDie.getLabel(), AddDie.getx()+5, AddDie.gety()+50, paint);
        
        g.drawString(String.valueOf(g.getWidth()/5), 500, 500, paint);     
        
        
        g.drawRect(Bin.getx(), Bin.gety(), Bin.getwidth(), 
        		Bin.getheight(), Color.RED);
        
        //        Assets.background = g.newImage("background.jpg", ImageFormat.RGB565);
        //        g.drawImage(Assets.background, 50, 50);
        // g.drawRect(0, 0, g.getWidth(), g.getHeight(), 100);

        // First draw the game elements.

        // Example:
        // g.drawImage(Assets.background, 0, 0);
        // g.drawImage(Assets.character, characterX, characterY);

        // Secondly, draw the UI above the game elements.
        if (state == GameState.Ready)
            drawReadyUI();
        if (state == GameState.Running)
            drawRunningUI();
        if (state == GameState.Paused)
            drawPausedUI();
        if (state == GameState.GameOver)
            drawGameOverUI();

    }

    private void nullify() {

        // Set all variables to null. You will be recreating them in the
        // constructor.
        paint = null;

        // Call garbage collector to clean up memory.
        System.gc();
    }

    private void drawReadyUI() {
        Graphics g = game.getGraphics();
        g.drawARGB(155, 0, 0, 0);
        g.drawString("Tap each side of the screen to move in that direction.",
                640, 300, paint);
    }

    private void drawRunningUI() {
        Graphics g = game.getGraphics();
        
        
        
        
        Assets.one = g.newImage("one.jpg", ImageFormat.RGB565);
        Assets.two = g.newImage("two.jpg", ImageFormat.RGB565);
        Assets.three = g.newImage("three.jpg", ImageFormat.RGB565);
        Assets.four = g.newImage("four.jpg", ImageFormat.RGB565);
        Assets.five = g.newImage("five.jpg", ImageFormat.RGB565);
        Assets.six = g.newImage("six.jpg", ImageFormat.RGB565);
        
        for(int n = 0; n < dice.size(); n++){
        	Dice die = dice.get(n);
        	if (die.get_val() == 1)
            	g.drawImage(Assets.one, die.getx(), die.gety());
        	else if (die.get_val() == 2)
            	g.drawImage(Assets.two, die.getx(), die.gety());
        	else if (die.get_val() == 3)
            	g.drawImage(Assets.three, die.getx(), die.gety());
        	else if (die.get_val() == 4)
            	g.drawImage(Assets.four, die.getx(), die.gety());
        	else if (die.get_val() == 5)
            	g.drawImage(Assets.five, die.getx(), die.gety());
        	else if (die.get_val() == 6)
            	g.drawImage(Assets.six, die.getx(), die.gety());
        }
    }

    private void drawPausedUI() {
        Graphics g = game.getGraphics();
        // Darken the entire screen so you can display the Paused screen.
        g.drawARGB(155, 0, 0, 0);
        g.drawString("Game paused", 640, 300, paint);

    }

    private void drawGameOverUI() {
        Graphics g = game.getGraphics();
        g.drawRect(0, 0, 1281, 801, Color.BLACK);
        g.drawString("GAME OVER.", 640, 300, paint);

    }

    @Override
    public void pause() {
        if (state == GameState.Running)
            state = GameState.Paused;

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

    @Override
    public void backButton() {
        pause();
    }
}